package com.blockmeta.bbs.minemodule.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.alibaba.android.arouter.facade.Postcard;
import com.blankj.utilcode.util.SpanUtils;
import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.pojo.BBSLatestPOJO;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by author on 2018/1/3.
 */

public class BBSCollectionListAdapter extends BaseAdapter<BBSLatestPOJO.ListBean, BaseViewHolder> {
    private Context mContext;
    private boolean isEditStatus;

    public void setEditStatus(boolean editStatus) {
        isEditStatus = editStatus;
    }

    public BBSCollectionListAdapter(int layoutResId, @Nullable List<BBSLatestPOJO.ListBean> data, Context mContext) {
        super(layoutResId, data);
        this.mContext = mContext;
    }

    @Override
    protected void convert(BaseViewHolder helper, final BBSLatestPOJO.ListBean item) {
        item.setShow(true);


        if (item.getIdtype().equals("tid")) {


            final String type = item.getType() == null ? "0" : item.getType();

            ToggleButton toggleButton = helper.getView(R.id.tg_edit_selector);
            item.setSelected(toggleButton.isSelected());
            toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    item.setSelected(isChecked);
                }
            });
            TextView title = helper.getView(R.id.bbs_item_title);
            if (isEditStatus) {
                toggleButton.setVisibility(View.VISIBLE);
            } else {
                toggleButton.setVisibility(View.GONE);
            }

            String attachment = item.getAttachment() == null ? "" : item.getAttachment();
            //TODO 服务器未传输此参数
            String digest = item.getDigest();
            digest = "0";
            switch (type) {
                case "0": //普通
                    if (attachment.equals("2")) {
                        title.setText(new SpanUtils()
                                              .append(item.getTitle() + " ")
                                              .appendImage(mContext.getResources().getDrawable(R.drawable.comment_list_pic), SpanUtils.ALIGN_CENTER)
                                              .create());

                    } else {
                        title.setText(item.getTitle());
                    }
                    break;
                case "1"://投票  绿色
                    initTag(item, title, digest, attachment, mContext.getResources().getDrawable(R.drawable.vote_lable));
                    break;
                case "2"://商品  黑色
                    initTag(item, title, digest, attachment, mContext.getResources().getDrawable(R.drawable.product_lable));
                    break;
                case "3"://悬赏   橘黄
                    initTag(item, title, digest, attachment, mContext.getResources().getDrawable(R.drawable.award_lable));
                    break;
                case "4"://活动  红色
                    if (item.getFid()==121){
                        title.setText(item.getTitle());
                    }else {
                        initTag(item, title, digest, attachment, mContext.getResources().getDrawable(R.drawable.activity_lable));
                    }
                    break;
                case "5"://辩论  //蓝色
                    initTag(item, title, digest, attachment, mContext.getResources().getDrawable(R.drawable.debet_lable));
                    break;
                default:
//                helper.setText(R.mID.bbs_item_title, item.getTitle());
                    title.setText(item.getTitle());
                    break;
            }
            TextView tv_content = helper.getView(R.id.bbs_item_content);
            tv_content.setText(item.getContent());

            ImageView view = helper.getView(R.id.bbs_item_icon);
//        Glide.with(mContext).load(item.getAuthor_info().getAvatar()).placeholder(R.drawable.yoona).into(view);
            Picasso.with(mContext).load(item.getAuthor_info().getAvatar()).placeholder(R.drawable.yoona).into(view);
            //转换时间
            String longToString = FormatUtil.StringDataToString(item.getLastpost() + "");
            helper.setText(R.id.bbs_item_author, item.getAuthor_info().getName() + "/" + longToString);
            helper.setText(R.id.bbs_item_count, item.getReplies() + "");
            LinearLayout view1 = helper.getView(R.id.item_bbs_main);
            view1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isEditStatus) return;


                    if (type.equals("4")) {
                        if (item.getFid()==121){
                            //AMA
                            RouterHelper.buildPath(RouterPath.BBS_AMA_EXTEND_ACTIVITY)
                                    .withString("tid",item.getTid()+"")
                                    .navigation(mContext);
                        }else {
                            //活动
                            RouterHelper.startActivity(mContext, RouterPath.ACTIVE_DETAIL_ACTIVITY, item.getTid() + "");
                        }
                    } else if (type.equals("3")) {
                        //悬赏
                        RouterHelper.buildPath(RouterPath.BBS_REWARD_ACTIVITY)
                                .withString("tid",item.getTid()+"")
                                .navigation(mContext);
                    } else if (type.equals("1")) {
                        //投票
                        RouterHelper.buildPath(RouterPath.BBS_VOTE_ACTIVITY)
                                .withString("tid",item.getTid()+"")
                                .navigation(mContext);
                    } else {
                        RouterHelper.buildPath(RouterPath.BBS_DETAIL_ACTIVITY)
                                .withString("tid",item.getTid()+"")
                                .navigation(mContext);
                    }

                }
            });
        } else {
            TextView title = helper.getView(R.id.bbs_item_title);
            title.setText(item.getTitle());
            LinearLayout view1 = helper.getView(R.id.item_bbs_main);
            view1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (item.getIdtype().equals("aid")) {
                        RouterHelper.startActivity(mContext,RouterPath.BBS_COLLEGE_DETAIL_ACTIVITY,item.getId()+"");
                    } else if (item.getIdtype().equals("news")) {
                        Postcard postcard = RouterHelper.buildPath(RouterPath.NEWS_INNER_DETAIL_ACTIVITY);
                        if (item.getPost_id() != null) {
                            postcard.withString(Constant.ONE,item.getPost_id());
                        } else {
                            postcard.withString(Constant.ZERO,item.getPost_name());
                        }
                        postcard.withString(Constant.INFO_USERNAME, item.getAuthor_info().getName());
                        postcard.navigation(mContext);
                    } else if (item.getIdtype().equals("bbs")) {
                        RouterHelper.buildPath(RouterPath.BBS_DETAIL_ACTIVITY)
                                .withString("tid",item.getId()+"")
                                .navigation(mContext);
                    } else if (item.getIdtype().equals("allow")) {
                        RouterHelper.startActivity(mContext,RouterPath.WEB_ACTIVITY,item.getUrl(),"学院详情");
                    }
                }
            });
        }
    }

    private void initTag(BBSLatestPOJO.ListBean item, TextView title, String digest, String attachment, Drawable resId) {
        if (attachment.equals("2")) {
            if (!digest.equals("0")) {
                title.setText(new SpanUtils()
                                      .appendImage(mContext.getResources().getDrawable(R.drawable.essence_lable), SpanUtils.ALIGN_CENTER)
                                      .appendImage(resId, SpanUtils.ALIGN_CENTER)
                                      .append(" " + item.getTitle() + " ")
                                      .appendImage(mContext.getResources().getDrawable(R.drawable.comment_list_pic), SpanUtils.ALIGN_CENTER)
                                      .create());

            } else {
                title.setText(new SpanUtils()
                                      .appendImage(resId, SpanUtils.ALIGN_CENTER)
                                      .append(" " + item.getTitle() + " ")
                                      .appendImage(mContext.getResources().getDrawable(R.drawable.comment_list_pic), SpanUtils.ALIGN_CENTER)
                                      .create());
            }
        } else {
            if (!digest.equals("0")) {
                title.setText(new SpanUtils()
                                      .appendImage(mContext.getResources().getDrawable(R.drawable.essence_lable), SpanUtils.ALIGN_CENTER)
                                      .appendImage(resId, SpanUtils.ALIGN_CENTER)
                                      .append(" " + item.getTitle() + " ")
                                      .create());

            } else {
                title.setText(new SpanUtils()
                                      .appendImage(resId, SpanUtils.ALIGN_CENTER)
                                      .append(" " + item.getTitle() + " ")
                                      .create());
            }
        }
    }

}
